package com.mw.job.ui.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.ZooKeeper;

/**
 * Created by mengdexuan on 2017/9/30 16:13.
 */
public class CuratorClient {

	private static CuratorFramework curatorClient;

	public static void connect(String zookeeperConnectionString){
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
		curatorClient = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
		curatorClient.getConnectionStateListenable().addListener(new SessionConnectionStateListener());
		curatorClient.start();
	}

	public static CuratorFramework curatorClient(){
		return curatorClient;
	}

	public static ZooKeeper zooKeeper(){
		try {
			ZooKeeper zk = curatorClient.getZookeeperClient().getZooKeeper();
			return zk;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void close(){
		curatorClient.close();
	}
}
